const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
const DonePlugin = require('./DonePlugin.js');

module.exports = {
  // 入口文件
  entry: './src/index.js',
  output: {
    filename: 'bundle.[hash].js',
    path: path.join(__dirname, '/dist')
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.json', '.js'],
    alias: {
      '@': path.resolve(__dirname, 'src/')
    }
  },
  module: {
    // 配置相应的规则
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader', 'postcss-loader']
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: {
              importLoaders: 1
            }
          },
          'less-loader',
          {
            loader: 'style-resources-loader',
            options: {
              patterns: path.resolve(__dirname, './src/style/comm.less')
            }
          }
        ]
      },
      {
        test: /\.(ttf|eot|woff|woff2|svg)/,
        include: path.resolve(__dirname, './public/iconfont'),
        use: ['file-loader']
      },
      {
        test: /\.(mp3|mp4)/,
        include: path.resolve(__dirname, './src/assets'),
        use: ['file-loader']
      },
      {
        test: /\.(png|svg|jpg|gif|jpeg)$/,
        include: path.resolve(__dirname, './src'),
        use: [
          {
            loader: 'file-loader',
            options: {
              limit: 10240,
              name: 'images/[hash].[ext]',
              publicPath: '/'
            }
          }
        ]
      },
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: 'babel-loader'
      },
      {
        test: /\.(ts|tsx)$/,
        exclude: /node_modules/,
        use: ['ts-loader']
      },
      {
        test: /\.md$/,
        use: [
          {
            loader: "html-loader"
          },
          {
            loader: path.resolve(__dirname, './markdown-loader.js')
          }
        ]
      }      
    ]
  },
  // 配置相应的插件
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, './public/index.html'),
      favicon: path.resolve(__dirname, './public/library.ico')
    }),
    new NodePolyfillPlugin(),
    new DonePlugin()
  ]
};
